const TypeModel = require('../do/models/type');
class TypeService {
    async getType(id) {
        if (!id) {
            return await TypeModel.findAll({ 'order': [['createdAt', 'DESC']] });
        }
        return await TypeModel.findOne({
            where: {
                id: id
            }
        })
    }
    async createType(typename, typeimg, intro) {
        let data = { typename, typeimg, intro }
        return await TypeModel.create(data);
    }
    async destroyType(id) {
        return await TypeModel.destroy({
            where: { id: id }
        })
    }
    async updateType(id, data) {
        let { typename, typeimg, intro } = data;
        if (typeimg) {
            return await TypeModel.update({
                typename: typename,
                typeimg: typeimg,
                intro: intro
            }, {
                where: { id: id }
            })
        } else {
            return await TypeModel.update({
                typename: typename,
                intro: intro
            }, {
                where: { id: id }
            })
        }

    }
}
module.exports = new TypeService();